import { createRouter, createWebHistory } from 'vue-router'
import MainLayout from '@/layouts/MainLayout.vue'
import AdminVehicleManagement from '@/views/admin/VehicleManagement.vue'
import AdminVehicleManagementSimple from '@/views/admin/VehicleManagement_simple.vue'

const routes = [
  {
    path: '/',
    component: MainLayout,
    children: [
      {
        path: '',
        redirect: '/vehicle/list'
      },
      {
        path: 'vehicle/list',
        name: 'VehicleList',
        component: () => import('@/views/vehicle/VehicleList.vue'),
        meta: { requiresAuth: false }
      },
      {
        path: 'vehicle/:id',
        name: 'VehicleDetail',
        component: () => import('@/views/vehicle/VehicleDetail.vue'),
        meta: { requiresAuth: false }
      },
      {
        path: 'order/create/:id',
        name: 'CreateOrder',
        component: () => import('@/views/order/CreateOrder.vue'),
        meta: { requiresAuth: true }
      },
      {
        path: 'order/list',
        name: 'OrderList',
        component: () => import('@/views/order/OrderList.vue'),
        meta: { requiresAuth: true }
      },
      {
        path: 'order/:id',
        name: 'OrderDetail',
        component: () => import('@/views/order/OrderDetail.vue'),
        meta: { requiresAuth: true }
      },
      {
        path: 'user/profile',
        name: 'UserProfile',
        component: () => import('@/views/user/Profile.vue'),
        meta: { requiresAuth: true }
      }
    ]
  },
  {
    path: '/admin',
    component: MainLayout,
    meta: { requiresAuth: true, requiresAdmin: true },
    children: [
      {
        path: '',
        redirect: '/admin/dashboard'
      },
      {
        path: 'dashboard',
        name: 'AdminDashboard',
        component: () => import('@/views/admin/Dashboard.vue'),
        meta: { requiresAuth: true, requiresAdmin: true }
      },
      {
        path: 'users',
        name: 'AdminUsers',
        component: () => import('@/views/admin/UserManagement.vue'),
        meta: { requiresAuth: true, requiresAdmin: true }
      },
      {
        path: 'vehicles',
        name: 'AdminVehicles',
        component: AdminVehicleManagement,
        meta: { requiresAuth: true, requiresAdmin: true }
      },
      {
        path: 'orders',
        name: 'AdminOrders',
        component: () => import('@/views/admin/OrderManagement.vue'),
        meta: { requiresAuth: true, requiresAdmin: true }
      }
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/user/Login.vue'),
    meta: { requiresAuth: false }
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('@/views/user/Register.vue'),
    meta: { requiresAuth: false }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const isAuthenticated = !!localStorage.getItem('token')
  const user = JSON.parse(localStorage.getItem('user') || '{}')
  const isAdmin = user.role === 'admin'
  
  if (to.meta.requiresAuth && !isAuthenticated) {
    // 需要登录但未登录
    next({ name: 'Login' })
  } else if (to.meta.requiresAdmin && !isAdmin) {
    // 需要管理员权限但不是管理员
    next({ name: 'VehicleList' })
  } else {
    next()
  }
})

export default router 